from flask import Blueprint,request,jsonify

from configure.db_configure import db 
from datetime import datetime
from service.info import info_service
from rule import task_rule

app = Blueprint(__name__,__name__)

@app.route('/info/save',methods=["post"])
def save():
    """ 设置资料
    ---
    tags: ["info"]
    parameters:
      - name: type
        in: formData
      - name: content
        in: formData
    responses:
      200:
        description: ""
    """
    type = request.form['type']
    content = request.form['content']

    entity = info_service.save(type,content)
    return jsonify(entity)
    

@app.route('/info/saves',methods=["post"])
def saves():
    """ 设置资料,多行content
    ---
    tags: ["info"]
    parameters:
      - name: type
        in: formData
      - name: contents
        in: formData
    responses:
      200:
        description: ""
    """
    type = request.form['type']
    contents = request.form['contents']

    entity = info_service.saves(type,contents)
    return jsonify(entity)
    

@app.route('/info/findTypeList',methods=["get"])
def findTypeList():
    """ 查询资料
    ---
    tags: ["info"]
    parameters: []
    responses:
      200:
        description: ""
    """
    typeSet = set()
    for step in task_rule.metaData()["steps"]:
      if "infoTypes" in step:
        for infoType in step["infoTypes"]:
          typeSet.add(infoType)

    return jsonify(list(typeSet))


@app.route('/info/findPage',methods=["get"])
def findPage():
    """ 查询资料
    ---
    tags: ["info"]
    parameters:
      - name: type
        in: query
      - name: isHistory
        in: query
      - name: pageNum
        in: query
      - name: pageSize
        in: query
    responses:
      200:
        description: ""
    """

    type =  request.args.get('type') 
    isHistory = int(request.args.get('isHistory'))
    pageSize = int(request.args.get('pageSize'))
    pageNum = int(request.args.get('pageNum'))

    if isHistory==1 :
      res = info_service.findPageHistory(type,pageNum,pageSize)
    else:
      res = info_service.findPage(type,pageNum,pageSize)
    return jsonify(res)

